var navigationStack = new Array();
var currentNavigationItem;

function setCard(cardId) {
    if (!currentNavigationItem) {
        currentNavigationItem = sink.Structure[0];
    }

    if (currentNavigationItem.id == cardId)
        return;

    backButton.setText(currentNavigationItem.text);
    backButton.setVisible(true);
    navigationStack.push(currentNavigationItem);

    currentNavigationItem = findNode(sink.Structure, cardId);

    cardController.setCard(currentNavigationItem.card, currentNavigationItem.animation || 'slide');
    navigationBar.setTitle(currentNavigationItem.text);
}

function previousCard() {
    currentNavigationItem = navigationStack.pop();
    cardController.setCard(currentNavigationItem.card, currentNavigationItem.animation || 'slide');
    navigationBar.setTitle(currentNavigationItem.text);

    previous = navigationStack.pop();

    if (previous) {
        backButton.setText(previous.text);
        backButton.setVisible(true);
        navigationStack.push(previous);
    }
    else
        backButton.setVisible(false);
}

function findNode(node, id) {
    for (var i = 0; i < node.length; i++) {
        if (node[i].id == id) return node[i];
    }
    return null;
}